package 数据结构.章节09;

import java.util.Stack;

/**
 * @author 挚爱之夕
 * @date 2021/10/14 - 10 - 14 - 16:08
 * @Description: 数据结构
 * @Version: 1.0
 */
public class _165_栈的排序 {
    public static void main(String[] args) {
        Stack<Integer> source = new Stack<>();
        source.push(3);
        source.push(5);
        source.push(2);
        source.push(1);
        source.push(4);
        Stack<Integer> target = stackSort(source);
        System.out.println(target);
        System.out.println(target.peek());
    }
    //从大到小，栈顶最大
    private static Stack<Integer> stackSort(Stack<Integer> source){
        Stack<Integer> target = new Stack<>();
        while(!source.isEmpty()){
            int v1 = source.pop();
            while(!target.isEmpty() && target.peek()>v1){
                source.push(target.pop());
            }
            target.push(v1);
        }
        return target;
    }

}
